<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script
      type="text/javascript"
      src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=CPxHyHH5XD8mUnmRPbv9BehycOEklp34"
    ></script>
    <title>文本插入</title>
    <style>
      body,
      html,
      #allmap {
        width: 100%;
        height: 100%;
        overflow: hidden;
        margin: 0;
        font-family: "微软雅黑";
      }
    </style>
  </head>
  <body>
    <div id="allmap"></div>
  </body>
</html>
<script>
  const bmap = new BMapGL.Map("allmap");
  const point = new BMapGL.Point(116.404, 39.915);
  bmap.centerAndZoom(point, 12);
  bmap.enableScrollWheelZoom();

  // 创建文本标注
  const label = new BMapGL.Label("hello", {
    position: point, // 设置标注的地理位置
    offset: new BMapGL.Size(20, 20), // 设置标注的偏移量
  });
  // 设置label的样式
  label.setStyle({
    width: "50px",
    height: "20px",
    padding: "5px",
    color: "#fff",
    textAlign: "center",
    fontSize: "16px",
    background: "red",
    whiteSpace: "wrap",
    overflow: "hidden",
    lineHeight: "20px",
  });

  // 监听事件
  label.addEventListener("click", function (e) {
    alert(e.target.content);
  });
  bmap.addOverlay(label);
</script>
